package com.qing.demo.netty.demo1.aio;

import com.qing.demo.netty.demo1.aio.server.AioServer;

import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @author caozhipeng
 * @date 2025/7/22
 */
public abstract class ChannelInitializer implements CompletionHandler<AsynchronousSocketChannel, AioServer> {

    @Override
    public void completed(AsynchronousSocketChannel asynchronousSocketChannel, AioServer aioServer) {
        try {
            initChannel(asynchronousSocketChannel);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
//            等待下一个连接
            aioServer.serverSocketChannel().accept(aioServer, this);
        }
    }

    @Override
    public void failed(Throwable throwable, AioServer aioServer) {
        throwable.getStackTrace();
    }

    public abstract void initChannel(AsynchronousSocketChannel asynchronousSocketChannel);
}
